{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d89e676b",
   "metadata": {},
   "source": [
    "# 字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9ad6ac5",
   "metadata": {},
   "source": [
    "## 字典常见的三种创建方式"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7486cf5e",
   "metadata": {},
   "source": [
    "* info = {key1:value1 , key2:value2 , ...}直接按照字典格式创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "77dd0649",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'qian', 'stu10002': 'jin', 'stu10003': 'kai'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "info = {\n",
    "    \"stu10001\":\"qian\",\n",
    "    \"stu10002\":\"jin\",\n",
    "    \"stu10003\":\"kai\",\n",
    "#     \"stu10004\":\"kai\",\n",
    "}\n",
    "print(info)\n",
    "print(type(info))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2be0f9e3",
   "metadata": {},
   "source": [
    "* info_02 = dict(key1=value1 , key2=value2 , ... ) 使用dict()进行字典创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "91fa82ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'qian', 'stu10002': 'jin', 'stu10003': 'kai'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "info_02 = dict(\n",
    "    stu10001=\"qian\",\n",
    "    stu10002=\"jin\",\n",
    "    stu10003=\"kai\",\n",
    ")\n",
    "print(info_02)\n",
    "print(type(info_02))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7981c305",
   "metadata": {},
   "source": [
    "* 运用字典增加的方式新建字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "039299bb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'qian', 'stu10002': 'jin', 'stu10003': 'kai'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "info_03 = {} # 新建空字典\n",
    "info_03[\"stu10001\"] = \"qian\"\n",
    "info_03[\"stu10002\"] = \"jin\"\n",
    "info_03[\"stu10003\"] = \"kai\"\n",
    "print(info_03)\n",
    "print(type(info_03))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd5b1f38",
   "metadata": {},
   "source": [
    "## 字典的增、删、改、查"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d1d6f02",
   "metadata": {},
   "source": [
    "### 1.增"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "74c951e3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'qian', 'stu10002': 'jin', 'stu10003': 'kai', 'stu1804': 'Zhichao'}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi',\n",
       " 'stu1802': 'XiaoFeng',\n",
       " 'stu1803': 'LiuYu',\n",
       " 'stu1804': 'Zhichao'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info[\"stu1804\"] = \"Zhichao\"\n",
    "print(info)\n",
    "{'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu', 'stu1804': 'Zhichao'}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa726e37",
   "metadata": {},
   "source": [
    "### 2.删"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "473f7a3b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'qian'}\n"
     ]
    }
   ],
   "source": [
    "info_03.pop(\"stu10002\")\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba5b6888",
   "metadata": {},
   "source": [
    "### 3.改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "cd02145d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'qian', 'stu10002': 'jin'}\n"
     ]
    }
   ],
   "source": [
    "if \"stu10003\" in info_03:\n",
    "    info_03 [\"stu10003\"] = \"apex\"\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "767486dd",
   "metadata": {},
   "source": [
    "### 4.查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4faf9b53",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "查询的key不存在\n"
     ]
    }
   ],
   "source": [
    "print(\"stu10003\" in info_03)\n",
    "if \"stu10006\" in info_03:\n",
    "    print(info_03[\"stu10006\"])\n",
    "else:\n",
    "    print(\"查询的key不存在\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c8e8691b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
